from os import getlogin
from cloudcontrol._roles import Role

__all__ = [
    "Role",
    "localuser",
    
    # DEPRECATED: do not use these, they will be removed in a future release
    "local",
    "User"
]

localuser = Role("%(local_username)s@localhost" % dict(local_username = getlogin()))
"""represents the local user running on the local machine"""

# make sure the localuser sits in its own password group
localuser.configure(password_group = "cloudcontrol.localuser")

##########################################################################################
# DEPRECATION CODE

class _DeprecatedUserClassProxy(object):
    """internal helper class to force direct usage of User
    objects to issue deprecation warnings"""
    
    def __init__(self, connstring_or_user):
        self.__role = Role(connstring_or_user)
    
    def _issue_warning(self):
        import sys
        print >> sys.stderr, "DEPRECATION WARNING: please do not use the cloudcontrol.User class.  Use Roles instead."
    
    def __getattr__(self, attrname):
        self._issue_warning()
        return getattr(self.__role, attrname)
    

class _LocalObjectDeprecationUserProxy(object):
    """internal helper class to force any usage of the old 'local' user
    instance to issue deprecation warnings"""
    
    def __init__(self, user):
        self.__user = user
    
    def _issue_warning(self):
        import sys
        print >> sys.stderr, "DEPRECATION WARNING: the user 'cloudcontrol.local' is deprecated.  Please use 'cloudcontrol.localuser' instead."
    
    def __getattr__(self, attrname):
        self._issue_warning()
        return getattr(self.__user, attrname)
        

local = _LocalObjectDeprecationUserProxy(localuser)
"""provided for backwards-compatibility only, deprecation notices will be issued"""

User = _DeprecatedUserClassProxy
"""ensure that people don't try to use User objects directly anymore"""
